package com.yycwxt.modules.admin.cgj_xscb_gdpdk.gdpdk;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yycwxt.common.BaseService;
import com.yycwxt.modules.admin.cgj_xscb_gdpdk.xsckmxcb.Xsckcb;
import com.yycwxt.modules.admin.data_import.AccountData;
import com.yycwxt.modules.admin.data_import.ImpFile;
import com.yycwxt.modules.admin.data_import.ImpFileMapper;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by Mr.Zhang on 2022/9/17 15:02
 */
@Service
@Transactional
public class ImpGdpdkService extends BaseService<ImpFileMapper, ImpFile> {

    private final ImpGdpdkMapper impGdpdkMapper;

    public ImpGdpdkService(ImpGdpdkMapper impGdpdkMapper) {
        this.impGdpdkMapper = impGdpdkMapper;
    }

    public List<ImpFile> selectImpFiles(int zq) {
        return impGdpdkMapper.selectImpFiles(zq);
    }

    @SneakyThrows
    @Transactional
    public void impGdpdk(MultipartFile file) {
        EasyExcel.read(file.getInputStream(), Dpdk.class, new ReadListener<Dpdk>() {

            private static final int BATCH_SIZE = 1000;

            @Override
            public void invoke(Dpdk data, AnalysisContext context) {

                System.out.println(data);

                // 执行存储过程
                impGdpdkMapper.impGdpdk(
                        AccountData.getAccountDate(),
                        data.getDp(),
                        data.getHtdk(),
                        data.getWxdk(),
                        data.getSr(),
                        data.getHz()
                );
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {

            }
        }).sheet().doRead();
    }
}
